package com.yueqian.bookshop.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.jdbc.StringUtils;
import com.yueqian.bookshop.bean.Book;
import com.yueqian.bookshop.bean.BookVo;
import com.yueqian.bookshop.bean.RtnInfo;
import com.yueqian.bookshop.mapper.BookMapper;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Book对应的Controller
 */
@RestController
@RequestMapping("/book")
public class BookController {
    @Resource
    private BookMapper bookMapper;
    /**
     * 添加图书信息
     * 请求方式：POST
     * 请求URL：/bookshop/book/add
     * 请求参数：表单
     * 响应结果：JSON
     * @param book
     * @return
     */
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public RtnInfo add(Book book) throws Exception{
        RtnInfo rtnInfo = new RtnInfo();
        System.out.println("--进入到图书添加后台："+book);
        // 非空检验
        if(StringUtils.isNullOrEmpty(book.getBookName())){
            rtnInfo.setCode(-1);
            rtnInfo.setMsg("图书名称不能为空！");
        }
        if(StringUtils.isNullOrEmpty(book.getBookType().getBooktypeId()+"")){
            rtnInfo.setCode(-1);
            rtnInfo.setMsg("图书类别不能为空！");
        }
        if(StringUtils.isNullOrEmpty(book.getBookPic())){
            rtnInfo.setCode(-1);
            rtnInfo.setMsg("图书封面不能为空！");
        }
        if(StringUtils.isNullOrEmpty(book.getAuthor())){
            rtnInfo.setCode(-1);
            rtnInfo.setMsg("图书作者不能为空！");
        }
        if(StringUtils.isNullOrEmpty(book.getPublishingHouse())){
            rtnInfo.setCode(-1);
            rtnInfo.setMsg("出版社不能为空！");
        }
        if(StringUtils.isNullOrEmpty(book.getPublishingDate())){
            rtnInfo.setCode(-1);
            rtnInfo.setMsg("出版日期不能为空！");
        }
        if(StringUtils.isNullOrEmpty(book.getPrice()+"")){
            rtnInfo.setCode(-1);
            rtnInfo.setMsg("图书价格不能为空！");
        }
        if(StringUtils.isNullOrEmpty(book.getDepict())){
            rtnInfo.setCode(-1);
            rtnInfo.setMsg("图书描述不能为空！");
        }
        // 访问数据库
        if(bookMapper.addBook(book)>0){
            rtnInfo.setCode(1);
            rtnInfo.setMsg("图书信息添加成功！");
        }else{
            rtnInfo.setCode(0);
            rtnInfo.setMsg("图书信息添加失败！");
        }
        return rtnInfo;
    }

    /**
     * 获取图书列表信息（支持关键字检索，支持分页显示）
     * 请求方式：GET
     * 请求URL：/bookshop/book/findAll/{keywords}/{currentPage}/{status}
     * 请求参数：表单
     * 响应结果：JSON
     * @param keywords：关键字，图书名称/图书作者
     * @param currentPage：当前页
     * @param status：上架状态：0-未上架，1-已上架，2-已下架，默认0
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "findAll/{keywords}/{currentPage}/{status}",method = RequestMethod.GET)
    public RtnInfo findAll(@PathVariable(value = "keywords",required = true)String keywords,
                           @PathVariable(value = "currentPage",required = true)Integer currentPage,
                           @PathVariable(value = "status",required = false)Integer status) throws  Exception{
        RtnInfo rtnInfo = new RtnInfo();
        // 非空检验
        if(StringUtils.isNullOrEmpty(keywords)|| keywords.equals("''")){
            keywords = "";
        }
        if(StringUtils.isNullOrEmpty(currentPage+"")){
            currentPage = 1;
        }
        // 设置分页
        PageHelper.startPage(currentPage,5);// 5:每页显示5条
        // 访问数据库
        BookVo book = new BookVo();
        book.setKeywords(keywords);
        book.setStatus(status);
        List<Book> books = bookMapper.findBookAll(book);
        // 封装分页信息
        PageInfo<Book> lists = new PageInfo<>(books,5);// 5：分页导航条页码个数
        // 封装信息到前端
        if(books!=null){
            rtnInfo.setCode(1);
            rtnInfo.setMsg("图书信息获取成功！");
            rtnInfo.setResult(lists);
        }else{
            rtnInfo.setCode(0);
            rtnInfo.setMsg("图书信息获取失败！");
        }

        return rtnInfo;
    }
    /**
     * 修改图书信息
     * 请求方式：POST
     * 请求URL：/bookshop/book/update
     * 请求参数：表单
     * 响应结果：JSON
     * @param book
     * @return
     */
    @RequestMapping(value = "update",method = RequestMethod.POST)
    public RtnInfo update(Book book) throws Exception{
        RtnInfo rtnInfo = new RtnInfo();
        System.out.println("--进入到图书修改后台："+book);
        // 非空检验
        if(StringUtils.isNullOrEmpty(book.getBookId()+"")){
            rtnInfo.setCode(-1);
            rtnInfo.setMsg("图书编号不能为空！");
        }
        if(StringUtils.isNullOrEmpty(book.getBookName())){
            rtnInfo.setCode(-1);
            rtnInfo.setMsg("图书名称不能为空！");
        }
        if(StringUtils.isNullOrEmpty(book.getBookPic())){
            rtnInfo.setCode(-1);
            rtnInfo.setMsg("图书封面不能为空！");
        }
        if(StringUtils.isNullOrEmpty(book.getAuthor())){
            rtnInfo.setCode(-1);
            rtnInfo.setMsg("图书作者不能为空！");
        }
        if(StringUtils.isNullOrEmpty(book.getPublishingHouse())){
            rtnInfo.setCode(-1);
            rtnInfo.setMsg("出版社不能为空！");
        }
        if(StringUtils.isNullOrEmpty(book.getPublishingDate())){
            rtnInfo.setCode(-1);
            rtnInfo.setMsg("出版日期不能为空！");
        }
        if(StringUtils.isNullOrEmpty(book.getPrice()+"")){
            rtnInfo.setCode(-1);
            rtnInfo.setMsg("图书价格不能为空！");
        }
        if(StringUtils.isNullOrEmpty(book.getDepict())){
            rtnInfo.setCode(-1);
            rtnInfo.setMsg("图书描述不能为空！");
        }
        // 访问数据库
        if(bookMapper.updateBook(book)>0){
            rtnInfo.setCode(1);
            rtnInfo.setMsg("图书信息修改成功！");
        }else{
            rtnInfo.setCode(0);
            rtnInfo.setMsg("图书信息修改失败！");
        }
        return rtnInfo;
    }

    /**
     * 删除图书信息
     * 请求方式：GET
     * 请求URL：/bookshop/book/delete/{bookId}
     * 响应结果：JSON
     * @param bookId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "delete/{bookId}")
    public RtnInfo delete(@PathVariable(value = "bookId",required = true)Integer bookId) throws Exception{
        RtnInfo rtnInfo = new RtnInfo();
        // 访问数据库
        if(bookMapper.deleteBook(bookId)>0){
            rtnInfo.setCode(1);
            rtnInfo.setMsg("图书信息删除成功！");
        }else{
            rtnInfo.setCode(0);
            rtnInfo.setMsg("图书信息删除失败！");
        }
        return rtnInfo;
    }

    /**
     * 图书上架和下架管理
     * 请求方式：GET
     * 请求URL：/bookshop/book/updateStatus/${bookId}/${status}
     * 响应结果：JSON
     * @param bookId
     * @param status
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "updateStatus/{bookId}/{status}",method = RequestMethod.GET)
    public RtnInfo updateStatus(@PathVariable(value = "bookId",required = true)Integer bookId,
                                @PathVariable(value = "status",required = true)Integer status) throws Exception{
        RtnInfo rtnInfo = new RtnInfo();
        // 访问数据库
        Book book = new Book();
        book.setBookId(bookId);
        book.setStatus(status);
        if(bookMapper.updateStatus(book)>0){
            rtnInfo.setCode(1);
            if(status==1){
                rtnInfo.setMsg("图书上架成功！");
            }else{
                rtnInfo.setMsg("图书下架成功！");
            }
        }else{
            rtnInfo.setCode(0);
            if(status==1){
                rtnInfo.setMsg("图书上架失败！");
            }else{
                rtnInfo.setMsg("图书下架失败！");
            }
        }
        return rtnInfo;

    }
}
